import axios from 'axios';
import qs from 'qs';
import { uuid } from './uuid';

const pointURL = 'http://d.pezy.cn/data/report/h5';
const http = axios.create();

http.interceptors.request.use(
    config => {
        config.headers['Content-Type'] = 'application/x-www-form-urlencoded';
        return config;
    },
    function(error) {
        // Do something with request error
        return Promise.reject(error);
    }
);

const parameter = (() => {
    let parameter = {};
    if (process.client) {
        const inBrowser = typeof window !== 'undefined';
        // const inWeex =
        //     typeof WXEnvironment !== 'undefined' && !!WXEnvironment.platform;
        // const weexPlatform = inWeex && WXEnvironment.platform.toLowerCase();
        const UA = inBrowser && window.navigator.userAgent.toLowerCase();
        // const isIE = UA && /msie|trident/.test(UA);
        // const isIE9 = UA && UA.indexOf('msie 9.0') > 0;
        // const isEdge = UA && UA.indexOf('edge/') > 0;
        const isAndroid = UA && UA.indexOf('android') > 0;
        const isIOS = UA && /iphone|ipad|ipod|ios/.test(UA);
        // const isChrome = UA && /chrome\/\d+/.test(UA) && !isEdge;
        // const isPhantomJS = UA && /phantomjs/.test(UA);
        // const isFF = UA && UA.match(/firefox\/(\d+)/);
        const screen = window.screen;

        let ver, type;

        // ie
        if (UA.includes('msie')) {
            ver = UA.match(/msie ([\d.]+)/)[1];
            type = 'IE';
        }
        // firefox
        else if (UA.includes('firefox')) {
            ver = UA.match(/firefox\/([\d.]+)/)[1];
            type = 'Firefox';
        }
        // Chrome
        else if (UA.includes('chrome')) {
            ver = UA.match(/chrome\/([\d.]+)/)[1];
            type = 'Chrome';
        }
        // Opera
        else if (UA.includes('opera')) {
            ver = UA.match(/opera.([\d.]+)/)[1];
            type = 'Opera';
        }
        // Safari
        else if (UA.includes('Safari')) {
            ver = UA.match(/version\/([\d.]+)/)[1];
            type = 'Safari';
        }

        parameter = {
            ua: UA,
            screen: `${screen.width}x${screen.height}`,
            vendor: type,
            vendorVersion: ver,
            isAndroid,
            isIOS
        };
    }
    return parameter;
})();

/**
 * 把url转化为对象方便埋点的时候进行获取后上报
 */
// function transObj() {
//     const keywords = window.location.search.substr(1);
//     const result = keywords.replace(/&/g, '","').replace(/=/g, '":"');
//     const reqDataString = '{"' + result + '"}';
//     const info = JSON.parse(reqDataString);

//     return info;
// }

class Point {
    h5PointPage(options) {
        const point = {
            ts: new Date().valueOf(),
            verCode: '1.0.0',
            devVendor: 'h5',
            type: 'h5_pv',
            ua: navigator.userAgent,
            url: encodeURIComponent(location.href),
            deviceId: uuid(),
            ...parameter,
            ...options
        };

        const info = qs.stringify(point);

        http.post(pointURL, info).catch(error => {
            console.log(error);
        });
    }

    h5PointAction(options) {
        const point = {
            ts: new Date().valueOf(),
            verCode: '1.0.0',
            devVendor: 'h5',
            type: 'h5_action',
            ua: navigator.userAgent,
            url: encodeURIComponent(location.href),
            deviceId: uuid(),
            ...parameter,
            ...options
        };

        const info = qs.stringify(point);

        http.post(pointURL, info).catch(error => {
            console.log(error);
        });
    }
}

export default function(ctx, inject) {
    inject('point', new Point());
}
